package com.javaweb.controller;

import com.javaweb.service.dto.UserConditionDto;
import com.javaweb.service.dto.UserDto;
import com.javaweb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/blog-api/v0.1/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 根据条件查询用户
        @PostMapping("/list")
    public ResponseEntity<List<UserDto>> list(@RequestBody UserConditionDto userCond) {
        List<UserDto> userList = userService.findUsersByCondition(userCond);
        return ResponseEntity.ok(userList);
    }

    // 新增用户
    @PostMapping("/add")
    public ResponseEntity<UserDto> add(@RequestBody UserDto userDto) {
        UserDto createdUser = userService.addUser(userDto);
        return ResponseEntity.ok(createdUser);
    }

    // 修改用户密码
    @PostMapping("/password/change")
    public ResponseEntity<Void> changePassword(@RequestParam String userId, @RequestParam String newPassword) {
        userService.changePassword(userId, newPassword);
        return ResponseEntity.ok().build();
    }
}
